# coding:utf-8
import os
from typing import Dict, Any, Callable, List
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QHBoxLayout

from qfluentwidgets import FluentIcon as FIF
from qfluentwidgets import InfoBar, InfoBarPosition

from common.utils.utils_log import AntLogger
from .template_page_view import BasePageTemplateView
from .common.translator import Translator
from .template.main_card_template import MainCardTemplate
from .template.data_classes import WidgetTemplateResultDto


class TemplateDemoView(BasePageTemplateView):
    """模板演示视图"""

    def __init__(self, parent=None):
        t = Translator()
        super().__init__("TemplateDemoView", parent)
        self.setObjectName("TemplateDemoView")
        
        # 创建动态表单演示
        self.setupDynamicFormDemo()
        
    def setupDynamicFormDemo(self):
        """设置动态表单演示"""
        # 创建一个水平布局来放置两个卡片
        levelMain_HLayout = QHBoxLayout()
        
        # 创建一个按钮容器
        button_container = QWidget()
        button_layout = QVBoxLayout(button_container)
        
        # 创建显示动态表单的按钮
        show_form_button = QPushButton("显示动态表单", button_container)
        show_form_button.clicked.connect(self.showDynamicForm)
        button_layout.addWidget(show_form_button)
        
        # 将按钮容器放入卡片中
        button_card = MainCardTemplate("表单操作", button_container, 1, self.view)
        
        # 将按钮卡片添加到水平布局中
        levelMain_HLayout.addWidget(button_card)
        
        # 使用 addKeyValueFormTemplate 添加一个表单到水平布局
        form_result = self.addKeyValueFormTemplate(
            title="动态表单示例",
            dataFun=self.getDynamicFormData,
            datParamFun=self.getBaseParam,
            stretch=1,
            layout=levelMain_HLayout,
            style_file=self.get_style_path("key_value_form_style.qss")
        )
        
        # 应用按钮卡片样式
        button_card_result = WidgetTemplateResultDto(
            inner=button_container,
            card=button_card,
            target_layout=levelMain_HLayout,
            style_file=self.get_style_path("button_card_style.qss")
        )
        button_card_result.apply_style()
        
        # 将水平布局添加到主布局
        self.addOutLayout(levelMain_HLayout)
        
    def getBaseParam(self) -> Dict[str, str]:
        """获取基本参数"""
        return {"param": "value"}
        
    def getDynamicFormData(self, params: Dict[str, str]) -> Dict[str, str]:
        """获取动态表单数据"""
        AntLogger.info(f"获取动态表单数据，参数: {params}")
        return {
            "姓名": "张三",
            "邮箱": "zhangsan@example.com",
            "年龄": "25",
            "性别": "男",
            "备注": "这是一个示例表单"
        }
        
    def showDynamicForm(self):
        """显示动态表单弹窗"""
        # 定义表单配置
        form_config = [
            {
                "type": "line_edit",
                "label": "姓名",
                "key": "name",
                "default": ""
            },
            {
                "type": "line_edit",
                "label": "邮箱",
                "key": "email",
                "default": ""
            },
            {
                "type": "slider",
                "label": "年龄",
                "key": "age",
                "options": {
                    "min_value": 1,
                    "max_value": 100,
                    "initial_value": 25
                }
            },
            {
                "type": "combo_box",
                "label": "性别",
                "key": "gender",
                "default": "男",
                "options": {
                    "items": ["男", "女", "其他"]
                }
            },
            {
                "type": "text_edit",
                "label": "备注",
                "key": "comments",
                "default": ""
            }
        ]
        
        # 使用 addCustomBox 方法显示动态表单
        result = self.addCustomBox(
            title="用户信息",
            form_config=form_config,
            on_submit=self.handleFormSubmit
        )
        
        # 输出结果信息
        AntLogger.info(f"表单结果: {result}")
        
    def handleFormSubmit(self, form_data: Dict[str, Any]):
        """处理表单提交"""
        AntLogger.info(f"表单提交数据: {form_data}")
        
        # 显示成功消息
        message = f"表单提交成功！\n姓名: {form_data.get('name')}\n邮箱: {form_data.get('email')}"
        InfoBar.success(
            title="成功",
            content=message,
            orient=Qt.Orientation.Horizontal,
            isClosable=True,
            position=InfoBarPosition.TOP,
            duration=3000,
            parent=self
        ) 